package com.manager.plat.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.manager.plat.dao.ILimitUserDao;
import com.manager.plat.vo.LimitUserVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class LimitUserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2647318582337354706L;

	private static Logger logger = Logger.getLogger(LimitUserAction.class);
	
	private ILimitUserDao limitUserDao;			//用户总量限制数据接口
	private List<LimitUserVo> limitUserVoList;	//总户总量限制信息列表
	private LimitUserVo limitUserVo;			//用户总量信息Vo
	private String actionPath;					//操作成功跳转路径
	
	public String getLimitUserList(){
		limitUserVoList = limitUserDao.getLimitUserList();
		return SUCCESS;
	}
	
	public String addLimitUser(){
		limitUserVo = null;
		return SUCCESS;
	}
	
	public String editLimitUser(){
		if(limitUserVo.getId() != 0){
			limitUserVo = limitUserDao.getLimitUserById(limitUserVo.getId());
		}
		return SUCCESS;
	}
	
	public String updateLimitUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		limitUserVo.setAdd_user(platUser.getId());
		if(limitUserVo.getId() == 0){
			//判断用户总量限制表是否已经存在，已经存在的话就不能再次新增
			int count = limitUserDao.insertLimitUser(limitUserVo);
			
			if(count > 0){
				logger.info(String.format("[%s]添加用户总量信息成功", platUser.getLogin_username()));
			}else{
				logger.info(String.format("[%s]添加用户总量信息失败", platUser.getLogin_username()));
			}
		}else {
			int count = limitUserDao.updateLimitUser(limitUserVo);
			if(count > 0){
				logger.info(String.format("[%s]更新用户总量信息成功", platUser.getLogin_username()));
			}else{
				logger.info(String.format("[%s]更新用户总量信息失败", platUser.getLogin_username()));
			}
		}
		
		actionPath = "limitUser/getLimitUserList.action";
		return SUCCESS;
	}
	
	public String deleteLimitUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		int count = limitUserDao.deleteLimitUser(limitUserVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除用户总量信息成功", platUser.getLogin_username()));
		}else{
			logger.info(String.format("[%s]删除用户总量信息失败", platUser.getLogin_username()));
		}
		
		actionPath = "limitUser/getLimitUserList.action";
		return SUCCESS;
	}
	
	
	public ILimitUserDao getLimitUserDao() {
		return limitUserDao;
	}
	public void setLimitUserDao(ILimitUserDao limitUserDao) {
		this.limitUserDao = limitUserDao;
	}
	public List<LimitUserVo> getLimitUserVoList() {
		return limitUserVoList;
	}
	public void setLimitUserVoList(List<LimitUserVo> limitUserVoList) {
		this.limitUserVoList = limitUserVoList;
	}
	public LimitUserVo getLimitUserVo() {
		return limitUserVo;
	}
	public void setLimitUserVo(LimitUserVo limitUserVo) {
		this.limitUserVo = limitUserVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
